<?php

namespace App\Console\Commands;

use App\Models\Project;
use App\Models\Team;
use App\Models\User;
use App\Models\Volunteer;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;

class TeamRewardTime extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'refreshTeamRewardTime';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        //
        Log::info('Rank Team Reward Time');

        $tList = Team::where('verify_status', 1)->get();
        foreach($tList as $team)
        {
            $reward = Volunteer::whereRaw("user_id in (select user_id from user_team where team_id={$team->id} and verify_status = 1)")->sum('reward_time');

            $team->fill(['reward_time'=>$reward]);

            if($team->save())
            {
                Log::info("success  teamId: {$team->id}, reward_time: {$reward}");
            }else
            {
                Log::info("error    teamId: {$team->id}, reward_time: {$reward}");
            }
        }

    }
}
